#define _CRT_SECURE_NO_WARNINGS 1
#include<climits>
class Solution {
public:
    long long maxRunTime(int n, vector<int>& batteries)
    {
        sort(batteries.begin(), batteries.end());
        long long sum = 0;
        int _min = INT_MAX;
        for (auto a : batteries)
        {
            _min = min(a, _min);
            sum += a;
        }
        long long left = _min, right = sum / n + 1;
        while (left < right)
        {
            // cout<<"left="<<left<<",right="<<right<<endl;
            long long mid = left + (right - left) / 2 + 1;
            long long total = 0;
            for (long long a : batteries)
            {
                total += min(a, mid);
            }
            if (n * (long long)mid <= total)
            {
                left = mid;
            }
            else
            {
                right = mid - 1;
            }
        }
        return left;
    }
};